#include "softwarelist.h"
#include "ui_softwarelist.h"
#include "rtfform.h"

softwareList::softwareList(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::softwareList)
{
    ui->setupUi(this);

    dbModel = new myDataModel(this,ui->table,getQuery());

    dbModel->setHeader(QStringList()<<"№"<<"Клиент"<<"ПО"<<"Количество"<<"Дата"<<"Примечание");


    ui->table->show();
    ui->table->scrollTo(dbModel->index(dbModel->rowCount()-1,0));

    connect(ui->table->selectionModel(), SIGNAL(currentRowChanged ( const QModelIndex &, const QModelIndex &)),
            this, SLOT(scurrentRowChanged( const QModelIndex &, const QModelIndex &)));

    ui->table->horizontalHeader()->setResizeMode(SDB_enumber,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(SDB_client,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(SDB_date,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(SDB_software,QHeaderView::Stretch);
    ui->table->horizontalHeader()->setResizeMode(SDB_quantity,QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(SDB_comment,QHeaderView::Stretch);

}

softwareList::~softwareList()
{
    delete ui;
    delete dbModel;
}

void softwareList::updateList()
{
    dbModel->setQuery(getQuery());
}

QString softwareList::getQuery()
{
    return "SELECT * FROM softwarelist";
}


void softwareList::scurrentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
    ui->clientEdit->setText(dbModel->getData(SDB_client));
    ui->softwareEdit->setText(dbModel->getData(SDB_software));
    ui->commentEdit->setPlainText(dbModel->getData(SDB_comment));
    ui->quantityBox->setValue(dbModel->getData(SDB_quantity).toInt());
}


void softwareList::on_delsearchButton_clicked()
{
    ui->searchEdit->clear();
    dbModel->setFilterFixedString("");
}

void softwareList::on_searchButton_clicked()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}

void softwareList::on_searchEdit_returnPressed()
{
    dbModel->setFilterFixedString(ui->searchEdit->text());
}


void softwareList::on_addButton_clicked()
{
    if(ui->softwareEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите ПО.");
        return;
    }
    QSqlQuery query;
    query.prepare("INSERT INTO softwarelist "
                  "VALUES (DEFAULT,:client,:software,:quantity,:date,:comment)");
    query.bindValue(":date",QDate::currentDate());
    query.bindValue(":client",ui->clientEdit->text());
    query.bindValue(":software",ui->softwareEdit->text());
    query.bindValue(":quantity",ui->quantityBox->value());
    query.bindValue(":comment",ui->commentEdit->toPlainText());
    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemAdded();

}


void softwareList::on_changeButton_clicked()
{
    if(ui->softwareEdit->text().isEmpty()){
        QMessageBox::warning(0,"Ошибка","Введите ПО.");
        return;
    }

    QSqlQuery query;
    query.prepare("UPDATE softwarelist "
                  "SET client=:client, software=:software, quantity=:quantity, comment=:comment "
                  "WHERE enumber=:number");
    query.bindValue(":client",ui->clientEdit->text());
    query.bindValue(":software",ui->softwareEdit->text());
    query.bindValue(":quantity",ui->quantityBox->value());
    query.bindValue(":comment",ui->commentEdit->toPlainText());
    query.bindValue(":number",dbModel->getData(SDB_enumber));


    query.exec();

    if(query.lastError().isValid())QMessageBox::warning(0,"Неверный запрос",query.lastError().text());

    dbModel->itemChanged();
}


void softwareList::on_printButton_clicked()
{
    if(ui->table->currentIndex().row()<0)return;

    QMap<QString,QString> formdata;
    formdata["<enumber>"]=dbModel->getData(SDB_enumber);
    formdata["<date>"]=dbModel->getData(SDB_date);
    formdata["<client>"]=dbModel->getData(SDB_client);
    formdata["<software>"]=dbModel->getData(SDB_software);
    formdata["<quantity>"]=dbModel->getData(SDB_quantity);
    rtfForm rtf(QApplication::applicationDirPath()+"\\software.rtf",formdata);

}
